from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)  # 邮箱字段
    password = db.Column(db.String(200), nullable=False)

    def __repr__(self):
        return f'<User {self.username}>'

class Message(db.Model):
    # 消息记录的唯一标识，自增长整数类型，为主键
    id = db.Column(db.Integer, primary_key=True)
    # 姓名，字符串类型，最大长度为 50，不能为空
    name = db.Column(db.String(50), nullable=False)
    # 身份证号，字符串类型，最大长度为 18，要求唯一且不能为空
    idcard = db.Column(db.String(18), unique=True, nullable=False)
    # 消息类型，字符串类型，最大长度为 20，不能为空
    type = db.Column(db.String(20), nullable=False)

    def __repr__(self):
        return f'<message {self.name}>'

class Appointment(db.Model):
    __tablename__ = 'appointments'
    id = db.Column(db.Integer, primary_key=True)
    spot_id = db.Column(db.Integer, db.ForeignKey('scenic_spots.id'), nullable=False)  # 外键关联景区表
    date = db.Column(db.Date, nullable=False)  # 预约日期
    time = db.Column(db.String(20), nullable=False)  # 时间段
    traveler_name = db.Column(db.String(255), nullable=False)  # 旅客姓名
    id_card = db.Column(db.String(18), nullable=False)  # 身份证号
    ticket_type = db.Column(db.String(50), nullable=False)  # 票型（如成人票、儿童票等）
    status  = db.Column(db.String(20), default="预约中") 

    # Relationships
    scenic_spot = db.relationship('ScenicSpot', backref=db.backref('appointments', lazy=True))

    def __repr__(self):
        return f"<Appointment(id={self.id}, spot_id={self.spot_id}, date={self.date}, traveler_name={self.traveler_name})>"
    
class Group(db.Model):
    __tablename__ = 'groups'
    id = db.Column(db.Integer, primary_key=True)
    spot_id = db.Column(db.Integer, db.ForeignKey('scenic_spots.id'), nullable=False)
    team_size = db.Column(db.Integer, nullable=False)
    contact_name = db.Column(db.String(255), nullable=False)
    contact_phone = db.Column(db.String(20), nullable=False)
    excel_file = db.Column(db.String(255), nullable=True)
    date = db.Column(db.Date, nullable=False, default='1970-01-01')
    time = db.Column(db.String(20), nullable=False, default="6:00-12:00")
    
    def __repr__(self):
        return f"<Group(id={self.id}, spot_id={self.spot_id}, team_size={self.team_size}, contact_name={self.contact_name})>"